package com.srd.phone.activity;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.shengrongdai.srd.R;
import com.srd.phone.bean.ResultInfo;
import com.srd.phone.bean.UserInfo;
import com.srd.phone.mvp.presenters.LoginPresenter;
import com.srd.phone.utils.Constants;
import com.srd.phone.utils.LogUtils;
import com.srd.phone.utils.PreferenceUtils;
import com.srd.phone.utils.UserUtils;
import com.srd.phone.utils.verifycode.CheckView;

/**
 * 登录界面
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private LoginPresenter loginPresenter;
    private String url = Constants.BASEURL + "loginapp/login"; //登陆
    private String verifyUrl = Constants.BASEURL + "loginapp/imgCode"; //验证码地址

    private EditText et_user;
    private EditText et_pwd;
    private ImageView iv_eye;
    private TextView tv_warn;
    private EditText et_verify;
    private Button bt_login;
    private TextView tv_register;
    private LinearLayout ll_verify;
    private CheckView mCheckView;
    private TextView tv_forget_pwd;

    private boolean hidePwd = true; //当前密码隐藏状态，true表示隐藏，
    private int loginCount = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initData();
    }

    public void initView() {
        tv_common_title.setText("登录");
        et_user = (EditText) findViewById(R.id.et_user);
        et_pwd = (EditText) findViewById(R.id.et_pwd);
        iv_eye = (ImageView) findViewById(R.id.iv_eye);
        iv_eye.setOnClickListener(this);
        et_verify = (EditText) findViewById(R.id.et_verify);
        bt_login = (Button) findViewById(R.id.bt_login);
        bt_login.setOnClickListener(this);
        tv_warn = (TextView) findViewById(R.id.tv_warn);
        tv_register = (TextView) findViewById(R.id.tv_register);
        tv_register.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        tv_register.getPaint().setAntiAlias(true);//抗锯齿
        tv_register.setOnClickListener(this);
        mCheckView = (CheckView) findViewById(R.id.checkView);
        mCheckView.setOnClickListener(this);
        ll_verify = (LinearLayout) findViewById(R.id.ll_verify);
        tv_forget_pwd = (TextView) findViewById(R.id.tv_forget_pwd);
        tv_forget_pwd.setOnClickListener(this);

    }

    public void initData() {
        presenter = new LoginPresenter(requestQueue, this);
        loginPresenter = (LoginPresenter) presenter;
        setUrlArrs(url, verifyUrl);
    }

    //登录
    private void loginSubmit() {
        String name = et_user.getText().toString().trim();
        String pwd = et_pwd.getText().toString().trim();
        String verify = et_verify.getText().toString().trim();
        String mLogin = loginPresenter.checkLogin(name, pwd, verify);
        if (TextUtils.isEmpty(mLogin)) {
            showWaitingDialog();
            tv_warn.setVisibility(View.INVISIBLE);
            loginPresenter.requestLogin(url, name, pwd, verify);
        } else {
            tv_warn.setVisibility(View.VISIBLE);
            tv_warn.setText(mLogin);
        }
    }

    //获取验证码
    private void getVerifyValue() {
        loginPresenter.getVerifyData(verifyUrl);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_login:
                loginSubmit();
                break;
            case R.id.tv_register:
                startActivity(new Intent(mContext, RegisterActivity.class));
                break;
            case R.id.checkView:
                getVerifyValue();
                break;
            case R.id.iv_eye:
                if (hidePwd) { //表示当前是隐藏密码的状态
                    et_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    hidePwd = false;
                } else {
                    et_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    hidePwd = true;
                }
                et_pwd.setSelection(et_pwd.getText().length());
                break;
            case R.id.tv_forget_pwd:
                startActivity(new Intent(mContext, RetrievePwdActivity.class));
                break;
        }
    }

    @Override
    public void returnData(String tag, String data) {
        super.returnData(tag, data);
        LogUtils.i(LoginActivity.class, tag + "   " + data);
        if (url.equals(tag)) { //登录返回值
            cancelWaitingDialog();
            UserInfo userinfo = UserInfo.parse(data);
            if (userinfo.code == -101) {
                PreferenceUtils.getInstance().putData(Constants.LOGIN_STATUS, true);
                userinfo.userName = et_user.getText().toString().trim();
                userinfo.pwd = et_pwd.getText().toString().trim();
                UserUtils.saveUserInfo(userinfo);
                startActivity(new Intent(mContext, MainActivity.class));
                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                finish();
                return;
            }
            tv_warn.setText(userinfo.msg);
            tv_warn.setVisibility(View.VISIBLE);
            if (userinfo.code == -102) { //用户名或密码错误
                loginCount++; //记录登录失败次数
                if (loginCount >= userinfo.baseCount) {
                    ll_verify.setVisibility(View.VISIBLE);
                    loginPresenter.isShowCheck = true;
                    getVerifyValue();
                }
            }
        } else if (verifyUrl.equals(tag)) { //验证码地址
            ResultInfo resultInfo = ResultInfo.parse(data);
            loginPresenter.setCheckNum(resultInfo.data, mCheckView);
        }
    }

    @Override
    public void onError(String tag, VolleyError error) {
        super.onError(tag, error);
        LogUtils.i(LoginActivity.class, error.statusCode + "   " + tag + "   " + error.toString());
    }
}